package com.css.fxfzaqbz.modules.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.common.utils.excel.ExportUtil;
import com.css.fxfzaqbz.modules.assess1.entity.AlgEntity;
import com.css.fxfzaqbz.modules.base.entity.HouseData;
import com.css.fxfzaqbz.modules.base.entity.JcEmergcyPlan;
import com.css.fxfzaqbz.util.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slw.common.helper.PropsHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 处置要点分析-地震应急预案
 */
@RestController
@RequestMapping("/base/jcEmergcyPlan")
@Slf4j
public class JcEmergcyPlanController {
    @Resource
    RestTemplateUtil restTemplateUtil;

    //分页条件查询
    @GetMapping("/page")
    public RestResponse page(JcEmergcyPlan jcEmergcyPlan) {
        RestResponse restResponse = null;
        try {
            Properties configGroup = PropsHelper.loadProps("slw-config.properties");
            String url = configGroup.getProperty("restTemplateUrl") + "/datacenter/jcEmergcyPlan/pageQuery";
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("jepCode", jcEmergcyPlan.getJepCode());
            paramMap.put("unitname", jcEmergcyPlan.getUnitname());
            paramMap.put("record", jcEmergcyPlan.getRecord());
            paramMap.put("member", jcEmergcyPlan.getMember());
            ResponseEntity<String> cchResponseEntity = restTemplateUtil.sendPostHttpRequest1(url, paramMap);
            List<JcEmergcyPlan> jcEmergcyPlans = JSONObject.parseArray(cchResponseEntity.getBody(), JcEmergcyPlan.class);
            restResponse = RestResponse.succeed(jcEmergcyPlans);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            if (StringUtils.isBlank(errormessage)) {
                errormessage = "查询失败";
            }
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //导出到excel
    @PostMapping("/export")
    public void export(JcEmergcyPlan jcEmergcyPlan, HttpServletResponse response) {
        try {
            List<JcEmergcyPlan> jcEmergcyPlans = Arrays.asList(jcEmergcyPlan);
            ExportUtil.createExcel(response, jcEmergcyPlans, "地震应急预案表", JcEmergcyPlan.class);
        } catch (Exception e) {
            log.error("导出失败:" + e.getMessage());
        }
    }
}
